//vtɨ "Race" for gvmaker.
//Copyright (C) 2008  KumaYu

#define DELAY_TIME 0 //pc=150~300
#define DoubleBuffer 1 

//Time
struct TIME
{
   int year;
   char month;
   char day;
   char hour;
   char minute;
   char second;
   char week;
};
struct TIME time;
int FPS[3];
int Time[2];
int MyLife=200;
int MyCarX=134;
char MyCarHit=0;
int MySPEED=100;
char Frame=0;
int  Dist[5];
char KeyLf;
char KeyUp;
char KeyRt;
char KeyDn;
char CarPose;
int X[5];
int Y[5];
int OverTake=0;
//================================================//
void GetFPS();
//================================================//
void main()
{

    int i,j;
    if (DoubleBuffer) 
    {  SetLCDMode(0); }
    SetBkMode(0);
    SetFgColor(YELLOW);
    SetBkColor(BLACK);    
    ClearScreen();
    //3 1 0 2 4
    Dist[0]=130;   
    Dist[1]=220;
    Dist[2]=190; 
    Dist[3]=160;
    Dist[4]=260; 
    
    ShowImage(BG01,0,0,320,220,Frame*320 ,0,0,0);
    TextOut(20,218,"Life");                           
    while(1)
    {
        //DRAW Background 
        if (MySPEED>=200)
        {  ShowImage(BG01,0,120,320,100,(Frame>>1)*640 ,120, 0,0); }
        else
        {  ShowImage(BG01,0,120,320,100,Frame*320 ,120, 0,0); }
        if (Frame<4) { Frame=Frame+1;}
        else {Frame=0;}
        
        //DRAW OtherCar1~5
        X[0]= 134;
        X[1]=  84+Dist[1]-Dist[1]/3 ;
        X[2]= 184-Dist[2]+Dist[2]/3 ;
        X[3]=  34+Dist[3]+Dist[3]/2-5;
        X[4]= 234-Dist[4]-(Dist[4]>>1)+5 ;        
        for(i=0; i<5 ; i++)
        {
            Y[i]=176-Dist[i];
            //DRAW Outline
            //Rectangle( x, y ,x+ 50, y+50,0);
            //DRAW CarNumber;
            //DigitOut(X[i] ,Y[i] ,Y[i]);
        } 
        if (Y[3]>100) {   ShowImage(Car01,X[3] ,Y[3] , 50,50, (Dist[3]>>2)*50 , 0 ,1,0); } 
        if (Y[1]>100) {   ShowImage(Car01,X[1] ,Y[1] , 50,50, (Dist[1]>>2)*50 , 0 ,1,0); } 
        if (Y[2]>100) {   ShowImage(Car01,X[2] ,Y[2] , 50,50, (Dist[2]>>2)*50 , 0 ,1,0); } 
        if (Y[0]>100) {   ShowImage(Car01,X[0] ,Y[0] , 50,50, (Dist[0]>>2)*50 , 0 ,1,0); } 
        if (Y[0]>100) {   ShowImage(Car01,X[4] ,Y[4] , 50,50, (Dist[4]>>2)*50 , 0 ,1,0); }                         
        
        
        
        //DRAW MyCar
        if (MyCarX<=20 || MyCarX>=250 || MyCarHit>0)
        {   
            ShowImage(Car01, MyCarX+rand()%4 ,176-rand()%2, 50,50, 0 , 0 ,1,0); 
            if (MySPEED>100) {MySPEED=MySPEED-5;}
            MyCarHit=0;
            if (Frame==0) {  MyLife=MyLife-rand()%2;  }
        }        
        else if (MyCarX<=40 || MyCarX>=230 || MySPEED>200)
        {   ShowImage(Car01, MyCarX+rand()%2 ,176, 50,50, 0 , 0 ,1,0);
        }
        else
        {  ShowImage(Car01, MyCarX ,176, 50,50, 0 , 0 ,1,0); }
        //DigitOut(MyCarX,150,MyCarX);
          
        //Draw Life
        SetFgColor(YELLOW);
        Rectangle( 59,220 ,261, 224,1);
        if (MyLife<200)
        {        
            SetFgColor(RED);
            Rectangle( 60+MyLife,221 ,260, 223,1);   
            SetFgColor(YELLOW);
        }
        
        
        //Reduce Life When "Crash"
        for(i=0; i<5 ; i++)
        { 
            if (Y[i]>=160 && Y[i]<=175 ) 
            {      
                X[i]=abs(X[i]-MyCarX);
                if (X[i] <25) 
                {  
                   if (MyLife>=0) { MyLife=MyLife-5-(MySPEED>>6); }
                   Dist[i]=Dist[i]+15;
                   MyCarHit=1;
                   if (MySPEED>100) { MySPEED=MySPEED-20-rand()%10; }
                }
            } 
        }   
          
        //Move EnemyCar 
        for(i=0; i<5 ; i++)
        { 
            if (Y[i]<116 && Frame>0)
            {   }        
             if (Y[i]<133 && Frame==0)
            {   }
            else if (Y[i]<133 && Frame==2)
            {   }
            else
            {
                Dist[i]=Dist[i]-((400-Dist[i])>>7);
            }
        }

        //Reborn EnemyCar
        if (Dist[0]<=0) {Dist[0]=130+rand()%10; OverTake=OverTake+1;}
        if (Dist[1]<=0) {Dist[1]=220+rand()%10; OverTake=OverTake+1;}
        if (Dist[2]<=0) {Dist[2]=190+rand()%10; OverTake=OverTake+1;}
        if (Dist[3]<=0) {Dist[3]=160+rand()%10; OverTake=OverTake+1;}
        if (Dist[4]<=0) {Dist[4]=260+rand()%10; OverTake=OverTake+1;}
        
        //Move MyCar
        //KeyBrake=CheckKey(20);
        KeyLf=CheckKey(20);
        KeyUp=CheckKey(21);
        KeyRt=CheckKey(22);
        KeyDn=CheckKey(23);
        ReleaseKey(128);      //MLA.         
        //uh۾10Pixel
        if      (KeyLf!=0 && MyCarX>=10)
        {   
            MyCarX=MyCarX-10;
            if (CarPose==2) {CarPose=1;}
        }
        //ukh۾k10Pixel
        else if (KeyRt!=0 && MyCarX<=260)
        {  
            MyCarX=MyCarX+10;
            CarPose=3;
        }
        
        if (MySPEED<250) 
        { MySPEED=MySPEED+(MySPEED>>6); }
        else if (MySPEED<270) 
        { MySPEED=MySPEED+1+rand()%2; }
        else if (MySPEED<299) 
        { MySPEED=MySPEED+rand()%2; }        
        else 
        {  MySPEED=300+rand()%2; }
        
        GetFPS();
        Delay(DELAY_TIME);
                
        if (DoubleBuffer) {   Refresh();  }
        if (MyLife<0) {break;}
    }
    SetFgColor(BLACK);
    Rectangle( 0,115 ,320, 145,1);
    SetFgColor(RED);
    Rectangle( 0,120 ,320, 140,1);
    SetFgColor(YELLOW);
    TextOut(90,122,"G a m e   O v e r");
    if (DoubleBuffer) {   Refresh();  }
}

//================================================//
void GetFPS()
{  
   int TimeGoesBy;

   GetTime (time);
   Time[0]=time.minute*60+time.second;
   FPS[0]=FPS[0]+1;   

   if (Time[1]==0)              //C}le, {bɶFPS[1]
   {  
       Time[1] = Time[0]; 
       FPS[1]=Time[1];
   }  
   else                           //YCwiF,
   {
       TimeGoesBy=Time[0]-Time[1];
              
       Time[1]=Time[0];
       
       TextOut(0,180,"FPS=");
       DigitOut(30,180,FPS[2]);
       if (Time[0]==FPS[1]+1)
       {   FPS[2]=FPS[0];
           FPS[1]=Time[0];
           FPS[0]=0;
       }
   }
   
   TextOut(0,166,"KM/H");
   DigitOut(35,166,MySPEED); 
   
   TextOut(0,152,"OverTake");
   DigitOut(68,152,OverTake);    
}